package com.lingxu.module.info.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.common.api.vo.ResultOne;
import com.lingxu.base.common.util.DateUtil;
import com.lingxu.base.config.LogInfoHandler;
import com.lingxu.base.system.fileManage.entity.SysFileInfo;
import com.lingxu.base.system.fileManage.service.SysFileInfoService;
import com.lingxu.base.system.manage.vo.LoginUser;
import com.lingxu.module.info.entity.*;
import com.lingxu.module.info.service.IInfoReleaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @Description: 信息发布
* @Author: 李德宁
* @Date:   2023-06-14
* @Version: V1.0.1
*/
@Slf4j
@Api(tags="信息发布")
@RestController
@RequestMapping("/info/infoRelease")
public class InfoReleaseController{
   @Resource
   private IInfoReleaseService infoReleaseService;
   @Autowired
   private SysFileInfoService sysFileInfoService;

   /**
    * 分页列表查询
    *
    * @param infoRelease
    * @return
    */
   @ApiOperation(value="信息发布-分页列表查询", notes="信息发布-分页列表查询")
   @PostMapping(value = "/getInfoReleasePageList")
   public ResultOne<InfoRelease> getInfoReleasePageList(@RequestBody InfoReleaseSearchVo infoRelease) {
       if(infoRelease.getPageNo()==null){
           infoRelease.setPageNo(1);
       }
       if(infoRelease.getPageSize()==null){
           infoRelease.setPageSize(6);
       }
       Page<InfoRelease> page = new Page<InfoRelease>(infoRelease.getPageNo(), infoRelease.getPageSize());
       IPage<InfoRelease> pageList = infoReleaseService.getInfoReleasePageList(page, infoRelease);
       return ResultOne.ok(pageList);
   }

    @ApiOperation(value="信息发布-根据ID获取详情", notes="信息发布-根据ID获取详情")
    @PostMapping(value = "/getInfoReleaseByID")
    public ResultOne<InfoRelease> getInfoReleaseByID(@RequestBody InfoRelease infoRelease) {
        InfoRelease infoRelease1=infoReleaseService.getById(infoRelease.getId());
        //查询文件信息
        Map<String, Object> map = new HashMap<>();
        map.put("ID",infoRelease.getFilePath());
        List<SysFileInfo> infos = sysFileInfoService.listByMap(map);
        infoRelease1.setFileInfoList(infos);
        return ResultOne.ok(infoRelease1);
    }

    @ApiOperation(value="信息发布-首页栏目类型查询", notes="信息发布-首页栏目类型查询")
    @PostMapping(value = "/getColumnInfoRelease")
    public Result<?> getColumnInfoRelease() {
        List<ColumnInfoRelease> list=infoReleaseService.getColumnInfoRelease();
        return Result.ok(list);
    }

   /**
    * 添加
    *
    * @param infoRelease
    * @return
    */
   @ApiOperation(value="信息发布-添加", notes="信息发布-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody InfoRelease infoRelease) {
       LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(StringUtils.isBlank(infoRelease.getType()) || StringUtils.isBlank(infoRelease.getColumntype())){
                return Result.error("请选择信息类型和栏目类型！");
        }
		infoRelease.setCreateby(user.getId());
		infoRelease.setCreatename(user.getName());
		infoRelease.setCreatetime(DateUtil.getDateTime());
        infoRelease.setArea(user.getArea());
		infoRelease.setRegion(user.getRegion());
        infoReleaseService.save(infoRelease);
        String content = "新增信息发布：【" + infoRelease.getTopic() +"】";
        new LogInfoHandler().setLogInfoHandler(2,2, content);
        return Result.ok("添加成功！");
   }

   /**
    * 编辑
    *
    * @param infoRelease
    * @return
    */
   @ApiOperation(value="信息发布-编辑", notes="信息发布-编辑")
   @PostMapping(value = "/edit")
   public Result<?> edit(@RequestBody InfoRelease infoRelease) {
       LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
       if(StringUtils.isBlank(infoRelease.getType()) || StringUtils.isBlank(infoRelease.getColumntype())){
           return Result.error("请选择信息类型和栏目类型！");
       }
       infoRelease.setCreateby(user.getId());
       infoRelease.setCreatename(user.getName());
       infoRelease.setCreatetime(DateUtil.getDateTime());
       infoRelease.setArea(user.getArea());
       infoRelease.setRegion(user.getRegion());
       infoReleaseService.updateById(infoRelease);
       String content = "修改信息发布：【" + infoRelease.getTopic() +"】";
       new LogInfoHandler().setLogInfoHandler(2,3, content);
       return Result.ok("编辑成功!");
   }


   /**
    * 批量删除
    *
    * @param info
    * @return
    */
   @ApiOperation(value="信息发布-单个删除", notes="信息发布-单个删除")
   @PostMapping(value = "/deleteById")
   public Result<?> deleteById(@RequestBody DeleteInfo info) {
       String id=info.getId();
       InfoRelease infoRelease=infoReleaseService.getById(id);
       String content = "删除信息发布：【" + infoRelease.getTopic() +"】";
       new LogInfoHandler().setLogInfoHandler(2,4, content);
       this.infoReleaseService.removeById(id);
       return Result.ok("删除成功！");
   }

    @ApiOperation(value="信息发布-批量删除", notes="信息发布-批量删除")
    @PostMapping(value = "/deleteByIds")
    public Result<?> deleteByIds(@RequestBody DeleteInfo info) {
       String ids=info.getIds();
       if(ids.contains(",")){
            List<String> idList = Arrays.asList(ids.split(","));
           String content = "批量删除信息发布：【" ;
            for(String id:idList){
                InfoRelease infoRelease=infoReleaseService.getById(id);
                content+=" "+infoRelease.getTopic()+" ";
            }
            content+="】";
           new LogInfoHandler().setLogInfoHandler(2,4, content);
           this.infoReleaseService.removeByIds(idList);
       }else{
           InfoRelease infoRelease=infoReleaseService.getById(ids);
           String content = "删除信息发布：【" + infoRelease.getTopic() +"】";
           new LogInfoHandler().setLogInfoHandler(2,4, content);
           this.infoReleaseService.removeById(ids);
       }
        return Result.ok("删除成功！");
    }

    @ApiOperation(value="信息发布-获取地区", notes="信息发布-获取地区")
    @PostMapping(value = "/getInfoReleaseArea")
    public  Result<?> getInfoReleaseArea() {
        LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String region= user.getRegion();
        if(StringUtils.isBlank(region)){
            region="320000";
        }
        List<InfoReleaseArea> list=infoReleaseService.getInfoReleaseArea(region);
        return Result.ok(list);
    }

    /**
     * @Author ysj闫世杰
     * @Description 获取发布主页的信息公告
     * @Date 2023/7/27 15:30
     * @Param:
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @RequestMapping(value = "/findHomeIndexInfos")
    public Result findHomeIndexInfos(){
       return infoReleaseService.findHomeIndexInfos();
    }
}
